/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package gui.forums.addedit;

import domain.DomainController;
import domain.forumdata.Category;
import java.awt.Toolkit;
import javax.swing.DefaultComboBoxModel;
import persistence.net.URLChecker;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 */
public class CategoryFrame extends javax.swing.JFrame {

    private DefaultComboBoxModel comboModel;
    
    /** Creates new form CategoryFrame */
    public CategoryFrame()
    {
        initComponents();
        this.comboModel = new DefaultComboBoxModel();
        for(Category cat : DomainController.getInstance().getNotAssignedCategories())
        {
            this.comboModel.addElement(cat.getName());
        }
        this.cmbCategories.setModel(comboModel);
        this.setIconImage(Toolkit.getDefaultToolkit().getImage("/data/icon.png"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblCategoryName = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtURL = new javax.swing.JTextField();
        cmdOK = new javax.swing.JButton();
        cmbCategories = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add category");
        setAlwaysOnTop(true);
        setResizable(false);

        lblCategoryName.setText("Category name:");

        jLabel1.setText("\"New topic\" URL:");

        cmdOK.setText("OK");
        cmdOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdOKActionPerformed(evt);
            }
        });

        cmbCategories.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(lblCategoryName)
                            .add(jLabel1))
                        .add(37, 37, 37)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(cmbCategories, 0, 288, Short.MAX_VALUE)
                            .add(txtURL, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, cmdOK))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblCategoryName)
                    .add(cmbCategories, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(txtURL, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(cmdOK)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdOKActionPerformed

        this.setAlwaysOnTop(false);

        if(URLChecker.check(this.txtURL.getText(), true))
        {
            //All OK!
            DomainController.getInstance().addTempForumCategory(this.cmbCategories.getSelectedItem().toString(), this.txtURL.getText());
            this.dispose();
        }

        this.setAlwaysOnTop(true);

    }//GEN-LAST:event_cmdOKActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmbCategories;
    private javax.swing.JButton cmdOK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblCategoryName;
    private javax.swing.JTextField txtURL;
    // End of variables declaration//GEN-END:variables
}